package city.spring.modules.work.service.impl;

import city.spring.modules.work.entity.WorkScheduleShiftEntity;
import city.spring.modules.work.repository.WorkScheduleShiftRepository;
import city.spring.modules.work.service.WorkScheduleShiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Service：排班结果的上班班次（与排班方案的上班班次大致一样，但是用处不同，这里是做冗余）
 *
 * @author 侯坤林
 * @date 2020-06-24 10:10:46
 */
@CacheConfig(cacheNames = {WorkScheduleShiftServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class WorkScheduleShiftServiceImpl extends ServiceImpl<WorkScheduleShiftRepository, WorkScheduleShiftEntity> implements WorkScheduleShiftService {

    @Override
    public void saveWorkScheduleShift(WorkScheduleShiftEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateWorkScheduleShift(WorkScheduleShiftEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteWorkScheduleShift(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteWorkScheduleShift(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public Map<String, WorkScheduleShiftEntity> getScheduleShifts(String scheduleId) {
        return lambdaQuery()
                .eq(WorkScheduleShiftEntity::getWorkScheduleId, scheduleId)
                .list().stream()
                .collect(Collectors.toMap(WorkScheduleShiftEntity::getId, Function.identity()));
    }
}